package Utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class StringUtil {

	public static int countMatch(String a, String b){
		int count = 0;
		while (a.indexOf(b) >= 0){
			count ++;
			a = a.replaceFirst(b, "");
		}
		return count;
	}
	public static String convertDateForSQL(String input){
		String []arr = input.split(" ");
		String date = arr[0];
		String time = arr[1];
		return date.split("/")[2]+"-"+date.split("/")[1]+"-"+date.split("/")[0]+" "+time.split(":")[0]+":"+time.split(":")[1]+":00";
	}
	
	public static String convertDate(String input,String lang){
		String []arr = input.split(" ");
		if(arr.length==2){
			String date = arr[0];
			String time = arr[1];
			if(lang.equals("vn"))
				return time.split(":")[0]+":"+time.split(":")[1]+", "+
						date.split("-")[2]+"/"+date.split("-")[1]+"/"+date.split("-")[0];
			else
				return date.split("-")[1]+"/"+date.split("-")[2]+"/"+date.split("-")[0]+", "+
						time.split(":")[0]+":"+time.split(":")[1];
					
		}else{
			String date = input;
			if(lang.equals("vn"))
				return	date.split("-")[2]+"/"+date.split("-")[1]+"/"+date.split("-")[0];
			else
				return	date.split("-")[1]+"/"+date.split("-")[2]+"/"+date.split("-")[0];
		}
	}
	
	public static String getThumbPath(String path){
		String thum = path.substring(0, path.lastIndexOf("."))+"_thumb"+path.substring(path.lastIndexOf("."), path.length());
		return thum;
	}
	public String readFile(String path){
		BufferedReader in = null; 
		String content = "";
		try{	
			in =new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
			String line="";
			while((line=in.readLine())!=null){
				content+=line+"\n\r";
			}
		}catch (Exception e) {
			System.out.println("File not found: "+path);
		}finally{
			if(in!=null) try{in.close();}catch (Exception e) {
			}
		}
		return content;
	}
	public static String filterTitle(String title){
		String res = title.toLowerCase();
		String arr[]={"`","~","!","@","#","$","%","^",
					  "&","*","(",")","+","[","]","{",
					  "}","|","\"","'",">","<","?","/",",",";"};
		for(int i=0; i < arr.length; i++){
			res = res.replace(arr[i], "");
		}
		res = res.replace(" ", "-");
		return res;
	}
	
	
}
